﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace DataAccessLayer.DAL
{
    public class AuctionDAL
    {
        public static List<Auction> GetAllWaittingAuction()
        {
            List<Auction> list = new List<Auction>();
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.Where(u => u.Status == (int)AuctionStatus.Watting)
                    .OrderBy(u => u.EndTime);
                foreach (Auction auc in result)
                {
                    list.Add(auc);
                }
            }
            return list;
        }

        public static List<Auction> GetAllOpenAuction()
        {
            List<Auction> list = new List<Auction>();
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.Where(u=>u.Status==(int)AuctionStatus.Active)
                    .OrderBy(u=>u.EndTime);
                foreach (Auction auc in result)
                {
                    list.Add(auc);
                }
            }
            return list;
        }

        public static List<Auction> GetAllEndAuction()
        {
            List<Auction> list = new List<Auction>();
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.Where(u => u.Status == (int)AuctionStatus.Endtime)
                    .OrderBy(u => u.EndTime);
                foreach (Auction auc in result)
                {
                    list.Add(auc);
                }
            }
            return list;
        }

        public static List<Auction> GetOpenAuctionByCategory(int id)
        {
            List<Auction> list = null;
            using (var context = new ReverseAuctionEntities())
            {
                string sql = string.Format("it.CategoryID=={0} and it.Status=={1}",id,(int)AuctionStatus.Active);
                list = context.Auctions.Where(sql).OrderBy("it.EndTime").ToList();
            }
            return list;
        }

        public static List<Auction> GetEndAuctionByCategory(int id)
        {
            List<Auction> list = new List<Auction>();
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.Where(u => u.Status == (int)AuctionStatus.Endtime && u.CategoryID==id)
                    .OrderBy(u => u.EndTime);
                foreach (Auction auc in result)
                {
                    list.Add(auc);
                }
            }
            return list;
        }

        public static List<Auction> GetAuctionByString(string str, int AuctionStatus)
        {
            List<Auction> list = null;
            using (var context = new ReverseAuctionEntities())
            {
                string sql = string.Format("it.Status=={0} and (it.Title LIKE '%{1}%' or it.Subtile LIKE '%{1}%' or it.Description LIKE '%{1}%')",
                    AuctionStatus, str);
                list = context.Auctions.Where(sql).OrderBy("it.EndTime").ToList();
            }
            return list;
        }

        public static Auction GetAuctionByID(int auctionID)
        {
            using (var context = new ReverseAuctionEntities())
            {
                return context.Auctions.SingleOrDefault(u => u.ID== auctionID);
            }
        }

        public static void CreateAuction(Auction auc, Dictionary<string, int> sellerlist, Dictionary<string, int> paymentlist)
        {
            
            using (var context = new ReverseAuctionEntities())
            {
                context.Auctions.AddObject(auc);
                context.SaveChanges();
                foreach (var item in sellerlist)
                {
                    var seller = context.Users.FirstOrDefault(u => u.ID == item.Value);
                    if (seller != null)
                    {
                        context.Users.ApplyCurrentValues(seller);
                        auc.Users.Add(seller);
                    }
                }
                context.Auctions.ApplyCurrentValues(auc);
                context.SaveChanges();
                foreach (var item in paymentlist)
                {
                    AuctionPaymentType PayType = new AuctionPaymentType();
                    PayType.PaymentTypeID = item.Value;
                    PayType.AuctionID = auc.ID;
                    context.AuctionPaymentTypes.AddObject(PayType);
                    context.SaveChanges();
                }
            }
        }

        public static void DeleteAuction(int auctionID)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.SingleOrDefault(u => u.ID == auctionID);
                result.Status = (int)AuctionStatus.Delete;
                context.SaveChanges();
            }
        }

        public static void ChangeStatusAuction(int auctionID, int status)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.SingleOrDefault(u => u.ID == auctionID);
                if (result != null)
                {
                    result.Status = status;
                    context.SaveChanges();
                }
            }
        }

        public static List<Auction> GetAllOpenAuctionIsUser( int id)
        {
            List<Auction> list = new List<Auction>();
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.Where(u => u.Status == (int)AuctionStatus.Active && u.CreateByUserID == id)
                    .OrderBy(u => u.StartTime);
             
                foreach (Auction auc in result)
                {
                        list.Add(auc);
                }
            }
            return list;
        }

        public static List<Auction> GetAllEndAuctionIsUser(int id)
        {
            List<Auction> list = new List<Auction>();
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Auctions.Where(u => u.Status == (int)AuctionStatus.Endtime && u.CreateByUserID == id)
                    .OrderBy(u => u.EndTime);
                foreach (Auction auc in result)
                {
                    
                    list.Add(auc);
                }
            }
            return list;
        }
    }
}
